clear all

* Set your directory path here
local root "D:\Understanding_Bank_Payouts"

capture log close
log using "`root'\Code\Logs\Final_code_RCFS\Figure3.log", replace
display "$S_TIME  $S_DATE"

set more off
set varabbrev off, perm

local finalfile_yearly "`root'\Data\Final\Finalfile_yearly.dta" 

capture noisily mkdir "`root'\Results"
capture noisily mkdir "`root'\Results\Final_code_RCFS"
cd "`root'\Results\Final_code_RCFS"

ssc install egenmore

use `finalfile_yearly'
xtset instkey year

drop if neverpayer == 1

generate divchg_text = ""
replace divchg_text = "Increase dividends" if divgrowthY1 > 0
replace divchg_text = "Decrease dividends" if divgrowthY1 < 0
replace divchg_text = "No change in dividends" if divgrowthY1 == 0

encode divchg_text, generate(divchg_category)

tabulate divchg_text 
tabulate divchg_category
tabout divchg_text using divchg_categories.csv if tarpever == 1, sum cells(mean divchg_category) style(csv) f(8) h3(" ")

egen return_tile = xtile(returnY), by(year) nq(100) 
egen roa_tile = xtile(avg_roaaWY), by(year) nq(100)

file open divs_badperf using "Figure3.txt", write replace

foreach perf in return roa {

	file write divs_badperf "Dividend policies of the worst performers over time - `perf' " _n(2)

	foreach y of varlist divgrowthY1  { 

		file write divs_badperf _tab "`y'" _n
		file write divs_badperf _tab "p1" _tab "p5" _tab "p10" _tab "p25" _tab "p33" _tab "p50" _tab "N" _n

		forvalues yr = 1995/2012 {
			file write divs_badperf "`yr'" 
			foreach k in 1 5 10 25 33 50 {
				summarize `y' if `perf'_tile <= `k' & year == `yr'
				file write divs_badperf _tab (r(mean)) 
				}
			summarize `y' if year == `yr'
			file write divs_badperf _tab (r(N))
			file write divs_badperf _n	
			}
		file write divs_badperf _n(3)	
	}

	}

file close divs_badperf

exit, clear
